<?php
/**
 * Copyright 2011  SURFfoundation
 * 
 * This file is part of ESCAPE.
 * 
 * ESCAPE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ESCAPE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ESCAPE.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * For more information:
 * http://escapesurf.wordpress.com/
 * http://www.surffoundation.nl/
 * 
 * Contact: d.vierkant@utwente.nl
 * 
 * @license http://www.gnu.org/licenses/gpl.html GNU GPLv3
 */
?>
<?php
/**
 * Create initial administrator
 */
class escape_setup_action_CreateInitialAdministrator extends escape_setup_Action
{
	public function handleRequest()
	{
		// create an initial administrator account based on the credentials specified in the config file
		
		$result = '';
		
		$repository = new escape_repository_Repository($this->requestAttributes['repositoryConfig']);
		$repository->initialize();
		
		// create initial administrator
		try
		{
			$result .= "Creating initial administrator\n\n";

			$user = $repository->createUser($repository->convertUserObjectIdToObjectUri($this->requestAttributes['repositoryConfig']['admin_username']));
			$user->setName('Administrator');
			$user->setMbox($this->requestAttributes['repositoryConfig']['admin_email']);
			$user->setPassword($this->requestAttributes['repositoryConfig']['admin_password']);
			$user->addSystemRole(escape_repository_User::$role_administrator);
			
			$result .= "Done!";
		}
		catch(Exception $exception)
		{
			$result .= "Failed to create the initial administrator account, it might already exist?";
		}
				
		$repository->shutdown();
		
		$this->requestAttributes['result'] = $result;

		return 'Result';
	}
}